package org.igeek.atomqq.adapter;

import org.igeek.atomqq.AtomQQApplication;
import org.igeek.atomqq.activity.BaseActivity;
import org.igeek.atomqq.domain.Friend;
import org.igeek.atomqq.domain.FriendCategory;
import org.igeek.atomqq.widget.FriendCategoryView;
import org.igeek.atomqq.widget.FriendItemView;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;

/** 
 * 好友分组适配器 
 * @author <a href="http://hangxin1940.cnblogs.com">hangxin1940@gmail.com</a>
 * @time 2012-2-2 下午05:27:30 
 */
public class FriendExpandListAdapter extends BaseExpandableListAdapter {

	private AtomQQApplication application;
	private BaseActivity context;
	
	public FriendExpandListAdapter(BaseActivity context) {
		application=(AtomQQApplication) context.getApplication();
		this.context=context;
	}
	
	@Override
	public int getGroupCount() {
		return application.getFriendCategoryCount();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return application.getFriendCategory(groupPosition).getFriendCount();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return application.getFriendCategory(groupPosition);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return application.getFriendCategory(groupPosition).getFriend(childPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		return application.getFriendCategory(groupPosition).getIndex();
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return application.getFriendCategory(groupPosition).getFriend(childPosition).getUin();
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		
		FriendCategory category=application.getFriendCategory(groupPosition);
		
		if(null==convertView){
			CategoryViewHolder holder=new CategoryViewHolder(context);
			holder.categoryView.setCategory(category);
			return holder.categoryView;
		}
		
		((FriendCategoryView)convertView).setCategory(category);
		
		return convertView;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		
		Friend friend=application.getFriendCategory(groupPosition).getFriend(childPosition);
		
		
		if(null==convertView){
			FriendViewHolder holder=new FriendViewHolder(context);
			holder.friendView.setFriend(friend);
			return holder.friendView;
		}
		
		((FriendItemView)convertView).setFriend(friend);
		
		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		
		return true;
	}
	
	static class CategoryViewHolder{
		static FriendCategoryView categoryView;
		public CategoryViewHolder(BaseActivity context) {
			categoryView=new FriendCategoryView(context);
		}
	}
	
	static class FriendViewHolder{
		static FriendItemView friendView;
		public FriendViewHolder(BaseActivity context) {
			friendView=new FriendItemView(context);
		}
	}

}
